package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.*;
import com.team.six.bluecatshop.service.*;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/shoppingcart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    ProductTypeService productTypeService;
    @Autowired
    ProductService productService;
    @Autowired
    CustomerService customerService;
    @Autowired
    SkuService skuService;
    @Autowired
    IndentService indentService;
    @Autowired
    ActivityService activityService;
    /**
     * 添加商品到购物车
     * */
    @GetMapping("/addcars")
    public String shoppingCars(String productId, Model model,String customerId,@RequestParam(value = "count",defaultValue = "1") String count, HttpSession session,String flag){
        if (flag.equals("1")){
            ShoppingCart shoppingCartProduct = shoppingCartService.findShoppingCartByProductId(productId);
            shoppingCartProduct.setProductCount(Integer.parseInt(count) + shoppingCartProduct.getProductCount());
            shoppingCartService.updateByPrimaryKey(shoppingCartProduct);
        }else {
            ShoppingCart shoppingCart=new ShoppingCart();
            shoppingCart.setProductId(productId);
            shoppingCart.setCustomerId(customerId);
            shoppingCart.setProductCount(Integer.parseInt(count));
            shoppingCartService.add(shoppingCart);
            int topCount = (int) session.getAttribute("count");
            topCount ++;
            session.setAttribute("count",topCount);//购物车小图标显示
        }
        return "redirect:/shoppingcart/cars";
    }

    /**
     * 判断该物品在购物车中是否有数据，若有则加数量，
     */
    @RequestMapping("/panduan")
    @ResponseBody
    public String panduan(String productId,@RequestParam(value = "count",defaultValue = "1") String count){
        int productCount = 0;
        ShoppingCart shoppingCartProduct = shoppingCartService.findShoppingCartByProductId(productId);
        if (shoppingCartProduct != null){
            productCount = Integer.parseInt(count) + shoppingCartProduct.getProductCount();
            //下面是限时购和普通商品的库存查询----------------------------------
            if (productCount > productService.findById(productId).getProductCount()){
               return "error";
            }else {
                return "success";
            }
        }else {
            return "none";
        }

    }

    /**
     * 显示购物车
     * */
    @GetMapping("/cars")
    public String showShoppingCars(Model model, HttpSession session){
        String customerId = (String) session.getAttribute("customerId");
        double priceAllShoppingCart = 0.00;
        List<ShoppingCart> shoppingCarts=shoppingCartService.findShoppingCartByCustomerId(customerId);
        for (ShoppingCart s:shoppingCarts) {
            String productId = s.getProductId();
            Product p = productService.findById(productId);

            //判断该商品是不是限时购商品
            if(activityService.findActivityByProductId(p.getProductId())!=null){
                Activity activity = activityService.findActivityByProductId(p.getProductId());
                p.setProductPrice(activity.getDiscountPrice());
            }
            //需要判断该商品是否是秒杀商品

            //计算所有购物车中所有的价格
            priceAllShoppingCart += p.getProductPrice().doubleValue()*s.getProductCount();
            p=productService.setImgToProduct(p);
            s.setProduct(p);
            s.setCustomer(customerService.findById(customerId));
        }
        model.addAttribute("priceAllShoppingCart",priceAllShoppingCart);
        model.addAttribute("shoppingCarts",shoppingCarts);
        return "shopping-cars";
    }

    /**
     * 删除购物车
     * */
    @GetMapping("/del_cars")
    @ResponseBody
    public String delCars(String cartId){
        shoppingCartService.remove(cartId);
        return "success";
    }

    @GetMapping("/multiDel")
    @ResponseBody
    public String multiDel(String ids){
        if(StringUtils.isNotBlank(ids)) {
            List<String> cartIds = Arrays.asList(ids.split(","));
            shoppingCartService.multiDel(cartIds);
        }
        return "success";
    }

    @RequestMapping("/goPay")
    public String goPay(String indentId,HttpSession session){
        //System.out.println("indentId = " + indentId);
        Indent indent = indentService.findById(indentId);
        session.setAttribute("indent",indent);
        return "redirect:/indent/shopping-order";
    }

}
